package com.dingwei.superiordiary.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.SignDayAdapter;
import com.dingwei.superiordiary.adapter.easyViewHolder.PointDetailsHolder;
import com.dingwei.superiordiary.bean.IntegerLogBean;
import com.dingwei.superiordiary.mvp.presenter.SignFragmentPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.ui.activity.IntegerMallActivity;
import com.dingwei.superiordiary.ui.activity.WebViewActivity;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.LazyLoadFragment;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.lihao.baselibrary.utils.L;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.lihao.baselibrary.base.BaseActivity.context;

/**
 * Created time 2018/2/27.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class SignFragment extends LazyLoadFragment implements Contract.SignFragmentView {
    SignFragmentPresenter signFragmentPresenter = new SignFragmentPresenter(this);
    @Bind(R.id.img_sign)
    ImageView imgSign;
    @Bind(R.id.tv_my_point)
    TextView tvMyPoint;
    @Bind(R.id.ll_Points)
    LinearLayout llPoints;
    @Bind(R.id.ll_help)
    LinearLayout llHelp;
    @Bind(R.id.ll_buy)
    LinearLayout llBuy;
    @Bind(R.id.mRecycleView)
    RecyclerView mRecycleView;
//    @Bind(R.id.sign_RecyclerView)
//    RecyclerView signRecyclerView;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    private int page = 1;

    RecyclerArrayAdapter adapter = new RecyclerArrayAdapter(context) {
        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            return new PointDetailsHolder(parent);
        }
    };
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    @Override
    protected int setContentView() {
        return R.layout.fragment_sign;
    }

    @Override
    protected void setBindView() {
        ButterKnife.bind(this, view);
        initView();
    }

    private void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycleView.setLayoutManager(layoutManager);
        mRecycleView.setNestedScrollingEnabled(false);
        mRecycleView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.white), DisplayUtil.dip2px(BaseActivity.context, 8)));
        mRecycleView.setAdapter(adapter);


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                signFragmentPresenter.initData(page, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                signFragmentPresenter.initData(page, true, false);
            }
        });

//        LinearLayoutManager layoutManager1 = new LinearLayoutManager(context);
//        layoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
//        signRecyclerView.setLayoutManager(layoutManager1);
//        signRecyclerView.setNestedScrollingEnabled(false);


    }

    @Override
    protected void lazyLoad() {
        page = 1;
        signFragmentPresenter.initData(page, true, true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.img_sign, R.id.ll_Points, R.id.ll_help, R.id.ll_buy,R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_sign:
                signFragmentPresenter.sign();
                break;
            case R.id.ll_Points:
                intent = new Intent(BaseActivity.context, WebViewActivity.class);
                intent.putExtra("title", "赚取次元币");
                intent.putExtra("url", URLS.HOST + URLS.GET_INTEGRAL);
                startActivity(intent);
                break;
            case R.id.ll_help:
                intent = new Intent(BaseActivity.context, WebViewActivity.class);
                intent.putExtra("title", "帮助说明");
                intent.putExtra("url", URLS.HOST + URLS.INTEGRAL);
                startActivity(intent);
                break;
            case R.id.ll_buy:
                intent = new Intent(BaseActivity.context, IntegerMallActivity.class);
                startActivity(intent);
                break;
            case R.id.no_net_btn:
                page = 1;
                signFragmentPresenter.initData(page, true, false);
                break;
        }
    }

    @Override
    public void signSuccess() {
        refreshLayout.autoRefresh();
    }

        public void showData(String state) {
            L.e("------");
                if (state.equals("success")) {
                    L.e("------");
                   refreshLayout.setVisibility(View.VISIBLE);
                   noNetLayout.setVisibility(View.GONE);
                } else {
                    refreshLayout.setVisibility(View.GONE);
                    noNetLayout.setVisibility(View.VISIBLE);
                }
            }

    @Override
    public void onRefresh(IntegerLogBean bean) {
        adapter.clear();
        adapter.addAll(bean.data.list);
        refreshLayout.finishRefresh();
        tvMyPoint.setText(bean.data.integral);

//        SignDayAdapter adapter = new SignDayAdapter(BaseActivity.context,bean.data.sign_day);
//        adapter.add("");
//        adapter.add("");
//        adapter.add("");
//        adapter.add("");
//        adapter.add("");
//        adapter.add("");
//        adapter.add("");
//        signRecyclerView.setAdapter(adapter);

        showData("success");

    }

    @Override
    public void onLoadMore(IntegerLogBean bean) {
        adapter.addAll(bean.data.list);
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void onFail() {
        showData("fail");
    }
}
